package com.admin.controller;

import com.admin.entity.Fee;
import com.admin.service.FeeService;
import com.admin.service.MedicalService;
import com.admin.entity.Medical;
import com.admin.entity.SysUser;
import com.admin.service.SysUserService;
import com.admin.sys.base.module.extend.web.BaseController;
import com.admin.sys.base.entity.ResultInfo;
import com.admin.sys.base.module.office.entity.SysOffice;
import com.admin.sys.base.module.office.service.SysOfficeService;
import com.admin.sys.utils.admin.ShiroUtils;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.base.entity.DateUtils;
import com.admin.sys.utils.excel.ExportExcel;
import com.admin.sys.utils.gen.GenUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
*【就诊信息】页面接口
*/
@RestController
@RequestMapping("/admin/medical")
public class MedicalController extends BaseController {

    @Autowired
    private MedicalService medicalService;
    @Autowired
    private FeeService feeService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysOfficeService sysOfficeService;

    /**
    * 【就诊信息】根据条件分页查询
    * @param page
    * @param medical
    * @return
    */
    @RequestMapping("/getPage")
    public ResultInfo getPage(Page<Medical> page, Medical medical) {
        //角色数据过滤
        if (ShiroUtils.getUserRole().contains("doctorRole")) {
            medical.setUserId(ShiroUtils.getUserInfo().getId());
        }
        IPage<Medical> iPage = medicalService.getPage(page, medical);
        return ResultInfo.ok("获取分页成功", iPage);
    }

    /**
    * 【就诊信息】根据条件查询
    * @param medical
    * @return
    */
    @RequestMapping("/getList")
    public ResultInfo getList(Medical medical) {
        List<Medical> list = medicalService.getList(medical);
        return ResultInfo.ok("获取列表成功", list);
    }

    /**
    * 【就诊信息】根据对象查询
    * @param medical
    * @return
    */
    @RequestMapping("/get")
    public ResultInfo get(Medical medical) {
        Medical entity = medicalService.get(medical);
        return ResultInfo.ok("获取对象成功", entity);
    }

    /**
    * 【就诊信息】提交(新增或修改)
    * @param medical
    * @return
    */
    @RequestMapping("/sub")
    public ResultInfo insert(Medical medical) {
        if (StringUtils.isEmpty(medical.getId())) { //新增
            medical.setUserId(ShiroUtils.getCurrentUserId());
            SysUser userInfo = ShiroUtils.getUserInfo();
            medical.setOfficeId(userInfo.getOfficeId());
            medicalService.insert(medical);
            //添加费用记录
            Fee fee = new Fee();
            fee.setFeeCode(GenUtils.getRuleNo("NO-",8));
            fee.setRegisterId(medical.getRegisterId());
            fee.setFeeType(1);//门诊
            fee.setAmount(medical.getFee());
            fee.setFeeDate(new Date());
            fee.setFeeStatus(1);
            feeService.insert(fee);
        } else {//修改
            medicalService.update(medical);
        }
        return ResultInfo.ok("提交成功!");
    }

    /**
    * 【就诊信息】删除
    * @param id
    * @return
    */
    @RequestMapping("/delete")
    public ResultInfo delete(String id) {
        medicalService.delete(id);
        return ResultInfo.ok("删除成功!");
    }

    /**
    * 【就诊信息】批量删除
    * @param ids
    * @return
    */
    @RequestMapping("/delAll")
    public ResultInfo delAll(String[] ids) {
        medicalService.delAll(ids);
        return ResultInfo.ok("批量删除成功！");
    }

}



